79B - Colorful Field - CodeForces Solution


implementation sortings *1400

Please click on ads to support us..

Python Code:

import math

numbers = list(map(int, input().split()))
rows    = numbers[0]
columns = numbers[1]
waste   = numbers[2]
queries = numbers[3]
fruite  = ['Carrots' , 'Kiwis' , 'Grapes']
indexs  = {}
arr     = []
for i in range ( 0 , waste) :
    x = list(map(int , input().split()))
    if not f'{x[0]}' in indexs :
        indexs[f'{x[0]}'] = len(arr)
        info = {
            'row' : x[0] ,
            'up'  : 0 ,
            'in'  : [x[1]]
        }
        arr.append(info)
    else :
        arr[indexs[f'{x[0]}']]['in'].append(x[1])
def re_row(key):
    return key['row']
arr.sort(key=re_row)
for i in range(0 , len(arr)):
    if i != 0 :
        arr[i]['up'] = arr[i-1]['up'] + len(arr[i - 1]['in'])
    indexs[f'{arr[i]["row"]}'] = i
def find_wast_rows(target , arr):
    start = 0
    end   = len(arr) - 1
    row    = 0
    while start <= end :
        middel = math.floor((start + end) / 2)
        if arr[middel]['row'] > target :
            if middel == 0 :
                row ='NONE'
                break
            if arr[middel - 1 ]['row'] < target:
                row = arr[middel - 1]['row']
                break
            else :
                end = middel - 1
        elif arr[middel]['row'] < target :
            if middel == len(arr) - 1 :
                row = arr[middel]['row']
                break
            if arr[middel + 1]['row'] > target :
                row = arr[middel]['row']
                break
            else :
                start = middel +1
        else :
            row = arr[middel]['row']
            break
    return row
def calc(wast_num , column_arr , row , column , columns_number):
    def the_num(row , columns_number , column , dif):
        x =  ( (row-1) * columns_number ) - dif
        y = column
        z = y + x - 1
        g = z % 3
        return g
    if wast_num == "NONE" :
        return  fruite[the_num(row , columns_number , column)]
    if len(column_arr) == 0 :
        return fruite[the_num(row , columns_number , column , wast_num)]
    else :
        column_arr.sort()
        start = 0
        end   = len(column_arr) -1
        needed_co = 0
        while (start <= end) :
            middel = math.floor((start + end) / 2)
            if column_arr[middel] > column :
                if middel == 0:
                    needed_co = column
                    break
                if column_arr[middel-1] < column:
                    needed_co= column - len(column_arr[slice(0 , middel )])
                    break
                else:
                    end = middel - 1
            elif column_arr[middel] < column:
                if middel == len(column_arr) - 1:
                    needed_co = column - len(column_arr)
                    break
                if column_arr[middel + 1]> column:
                    needed_co = column  - len(column_arr[slice(0 , middel+1)])
                    break
                else:
                    start = middel + 1
            else:
                needed_co = "Waste"
                break
        if needed_co == 'Waste' :
            return needed_co
        else :
            return fruite[the_num(row , columns_number , needed_co , wast_num)]
for i in range(0 , queries) :
    y = list(map(int , input().split()))
    wast   = find_wast_rows(y[0] , arr)
    column = []
    if wast == y[0] :
        column = arr[indexs[f'{wast}']]['in']
        wast    = arr[indexs[f'{wast}']]['up']
    else :
        if wast == "NONE" :
            wast = 0
        else :
            wast    = arr[indexs[f'{wast}']]['up'] + len(arr[indexs[f'{wast}']]['in'])
    print(calc(wast ,column, y[0] , y[1] , columns ))

C++ Code:

#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
using namespace std;

bool cmp(pair<int, int> a, pair<int, int> b) {
    if (a.first == b.first) {
        return a.second < b.second;
    }
    else return a.first < b.first;
}

int main() {
    // desPair...
    vector<pair<int, int>> wastePair;
    
    int n, m, k, t;
    cin >> n >> m >> k >> t;
    
    for (int i = 0; i < k; i++) {
        int a, b;
        cin >> a >> b;
        
        wastePair.push_back(make_pair(a, b));
    }
    
    sort(wastePair.begin(), wastePair.end(), cmp);
    
    for (int i = 0; i < t; i++) {
        int a, b;
        cin >> a >> b;
        
        bool isWaste = false;
        
        int blocks = (a - 1) * m + (b - 1);
        int waste = 0;
        
        for (pair<int, int> i : wastePair) {
            if (i.first == a && i.second == b) {
                cout << "Waste" << '\n';
                isWaste = true;
                break;
                
            }
            else if (i.first < a) {
                waste++;
            }
            else if (i.first == a && i.second < b) {
                waste++;
            }
        }
        
        
        
        if (isWaste) continue;
        
        int excludeWaste = blocks - waste;
        
        if (excludeWaste % 3 == 0) cout << "Carrots" << '\n';
        else if (excludeWaste % 3 == 1) cout << "Kiwis" << '\n';
        else if (excludeWaste % 3 == 2) cout << "Grapes" << '\n';
        
        // cout << waste << '\n';
    }
}


Comments

Submit
0 Comments
More Questions

96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram